#define _CRT_SECURE_NO_WARNINGS 1
#include<easyx.h>
#include<stdio.h>
#include<math.h>

#define BEAR_NUM 11
void puttransparentImage(int x, int y, const IMAGE* mask, const IMAGE* bear)
{
	putimage(x, y, mask, SRCAND);
	putimage(x, y, bear, SRCPAINT);
}

int main()
{
	initgraph(1200,480 );
	setbkcolor(WHITE);
	cleardevice();

	IMAGE bk;
	loadimage(&bk, "./picture/background.jpg");

	IMAGE Bear[BEAR_NUM];
	char imgBearPath[100];
	for (int i = 0; i < BEAR_NUM; i++)
	{
		sprintf(imgBearPath, "./picture/frames/bear%d.png", i);
		loadimage(&Bear[i], imgBearPath);
	}
	
	IMAGE Mask[BEAR_NUM];
	char imgMaskPath[100];
	for (int i = 0; i < BEAR_NUM; i++)
	{
		sprintf(imgMaskPath, "./picture/masks/bearmask%d.png", i);
		loadimage(&Mask[i], imgMaskPath);
	}

	int x=-150, y=180;
	int frame = 0;
	LARGE_INTEGER begin, end, F;
	QueryPerformanceFrequency(&F);


	BeginBatchDraw();
	while (1)
	{
		QueryPerformanceCounter(&begin);
		putimage(0, 0, &bk);
		puttransparentImage(x, y, &Mask[frame], &Bear[frame]);

		FlushBatchDraw();

		QueryPerformanceCounter(&end);

		frame++;
		x += 10;
		if (x >=1200+150) {
			x = -150;
		}
		if (frame >= BEAR_NUM) {
			frame = 0;
		}
		
		long long elapse = (end.QuadPart - begin.QuadPart) * 1000000 / F.QuadPart;

		while (elapse < 40000) {
			QueryPerformanceCounter(&end);
			elapse = (end.QuadPart - begin.QuadPart) * 1000000 / F.QuadPart;
		}

	}
	EndBatchDraw();
	getchar();

	closegraph();
	return 0;

}